{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import prerequisite packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import io\n",
    "import requests\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define helper method(s)\n",
    "\n",
    "`create_df_from_remote_csv` fetches a CSV file from a remote URL and creates a Pandas DataFrame from it. If an error is encountered (file not found, not a csv file, etc) the method returns None."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_df_from_remote_csv(url):\n",
    "    if url is None:\n",
    "        return None\n",
    "    response = requests.get(url)\n",
    "    if response.status_code == 200:\n",
    "        if response.headers['content-type'] == \"text/csv\":\n",
    "            response.encoding = 'utf-8'\n",
    "            data = pd.read_csv(io.StringIO(response.text))\n",
    "            return data\n",
    "        else:\n",
    "            print('Error. The file is encoded using unsupported content-type {}'\n",
    "                  .format(response.headers['content-type']))\n",
    "    else:\n",
    "        print('Error. The file could not be downloaded. Returned HTTP status code: {}'\n",
    "              .format(response.status_code))\n",
    "\n",
    "    return None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a Pandas DataFrame from a remote URL\n",
    "\n",
    "Load the Iris data set and create a DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = create_df_from_remote_csv(\"https://datahub.io/machine-learning/iris/r/iris.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Preview the DataFrame\n",
    "\n",
    "Display the first few rows of the data set if it was successfully loaded."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if df is not None:\n",
    "    # Print first few data rows\n",
    "    print(df.head())\n",
    "else:\n",
    "    print(\"Data file couldn't be loaded into a DataFrame.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Preview using the code snippet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TODO: insert the inspect_dataframe code snippet in this cell\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inspect_dataframe(df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc-showcode": false,
  "toc-showmarkdowntxt": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
